const express = require("express");
const ClassDB = require("../models/User");
const router = express.Router();

// 定义公共的返回对象
var returnData;
router.use(function (request,response,next) {
    returnData = {
        code:0,
        message:''
    };
    next();
});


// 已经定向到了views文件夹
// 默认 0；错误：-1 ， 2 ，3
// 注册成功：1  登录成功：0
// 后端登录接口
router.post('/user/login',function (request,response,next) {
    const UserData = request.body;
    if(UserData.account===''||UserData.password===''){
        returnData.code = -1;
        returnData.message = "错误：用户名或密码不能为空";
        response.json(returnData);
        return response;
    }
    ClassDB.findOne({
        username:request.body.account,
        password:request.body.password
    }).then(function (data) {
        console.log(data+'postlogin');// data返回findOne的结果
        if(!data){
            returnData.code = 3;
            returnData.message = "错误：用户名或者密码错误";
        }else{
            returnData.code = 0;
            returnData.message = "登录成功";
            // 登录用户直接验证权限，可能会有坑。但cookies验证有些顺序问题。
            returnData.userinfo = {
                _id:data._id,
                username:data.username,
                isAdmin:data.isAdmin
            };
        }
        request.cookies.set('userinfo',JSON.stringify({
            _id:data._id,
            username:data.username,
            isAdmin:data.isAdmin
        }));
        response.json(returnData);
        return response
    })
});

// 后端注册接口
router.post('/user/register',function (request,response,next) {
    // response.send("<h1>API</h1>");
    const UserData = request.body;
    if(UserData.account===''){
        returnData.code = -1;
        returnData.message = "错误：用户名不能为空";
        response.json(returnData);
        return response;
    }
    if(UserData.password===''){
        returnData.code = -1;
        returnData.message = "密码不能为空";
        response.json(returnData);
        return response;
    }
    if(UserData.repassword!==UserData.password){
        returnData.code = -1;
        returnData.message = "错误：密码不一致";
        response.json(returnData);
        return response;
    }
    // 数据库操作
    ClassDB.findOne({
        username:request.body.account
    }).then(function (data) {
        console.log(data+'regist');// data返回findOne的结果
        if(data){
            returnData.code = 2;
            returnData.message = "错误：用户名已被注册";
            response.json(returnData);
            return response
        }
        // 保存用户信息到数据库
        const user = new ClassDB({
            username:request.body.account,
            password:request.body.password
        });
        return user.save();
    }).then(function (newData) {
        // console.log(newData);
        returnData.message='注册成功!';
        returnData.code=1;
        response.json(returnData);
        return response;
    });

});

// 后端退出登录接口
router.post('/user/loginOut',function (request,response,next) {
    request.cookies.set('userinfo',null);
    response.json(returnData);
    return response;
});
module.exports = router;
